import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
//import './assets/theme/theme-green/index.css'
/*引入路由插件*/
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
/*引入axios模块*/
import axios from 'axios'
//给Vue实例添加原型
Vue.prototype.$http = axios;
Vue.prototype.$fastBaseUrl = "http://115.159.217.249:8888";
//只要是axios请求，在请求上都加上/api
axios.defaults.baseURL = "http://api.sunfulin.xyz";
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
/*定义路由映射组件*/
import routes from './routes'
// import Mock from './mock'
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });
const router = new VueRouter({
    routes//routes:routes
})

/*axios前置拦截器， 只要你发送axios之前，都先执行此箭头函数*/
axios.interceptors.request.use(config=>{
    //携带token
    let token =  localStorage.getItem("token");
    if(token){
        //添加请求头信息
        config.headers['token']=token;
    }
    return config;
},error => {
    Promise.reject(error);
})


//aixos后置拦截器，在响应目标函数之前，先执行此箭头函数
axios.interceptors.response.use(result=>{
    let data = result.data;
    if(!data.success&&data.message==="noLogin"){
        //把token和登录用户删除掉
        localStorage.removeItem("token");
        //把登录用户删除吊
        localStorage.removeItem("loginUser");
        //跳转到登录界面
        router.push("/login");
        return;
    }
    return result;
},error => {
    Promise.reject(error);
})

// 路由拦截器，暂时先不管
router.beforeEach((to, from, next) => {
    //如果你访问的界面是login或者商家入驻统一都应该放行
    if(to.path=="/login" || to.path=="/shopReg"){
        next();
    }else{
        //获取token的值
        var token = localStorage.getItem("token");
        //如果token有值，证明是登录状态
        if(token){
            //就应该是放行状态
            next();
        }else{
            //否则就应该跳转到登录界面
            next({path: '/login'});
        }
    }
})

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,//router:router
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

